import React, { useState, useEffect } from 'react'
import { useSelector } from 'react-redux';
import { useNavigate } from 'react-router-dom'; 
import { useDispatch } from 'react-redux';

export const Authority = (id, erID = "00") => {
  const [isAuth, setIsAuth] = useState(false);
  // const user = useSelector(state => state.user);
  // console.log(user)
  const navigate = useNavigate();
  const dispatch = useDispatch();

  useEffect(() => {
    const userInfo = localStorage.getItem('userInfo');
    const user = JSON.parse(userInfo);
    if (user?.value === id || user?.value === erID) {
      dispatch({ type: 'SET_USER_INFO', payload: user })
      setIsAuth(true);
    } else {
      navigate("/no-auth")
    }
  }, [dispatch, erID, id, navigate])

  return isAuth
}
